package com.tcr.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import com.tcr.common.ZipTask;
import com.tcr.core.ZipQueue;
import com.tcr.util.FileUtil;

/**
 * @author ylshao
 * 
 */
public class UploadAction extends ActionSupport {

	private static final int BUFFER_SIZE = 16 * 1024;
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String getUploadContentType() {
		return uploadContentType;
	}

	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	private File upload;

	private String uploadFileName;

	private String uploadContentType;

	private String c_dir;

	public String getC_dir() {
		return c_dir;
	}

	public void setC_dir(String c_dir) {
		this.c_dir = c_dir;
	}

	public String getC_version() {
		return c_version;
	}

	public void setC_version(String c_version) {
		this.c_version = c_version;
	}

	private String c_version;

	public String getPid() {
		return pid;
	}

	public void setPid(String pid) {
		this.pid = pid;
	}

	public String getUid() {
		return uid;
	}

	public void setUid(String uid) {
		this.uid = uid;
	}

	private String pid;
	private String uid;
	public String f_name;

	public String getF_name() {
		return f_name;
	}

	public void setF_name(String f_name) {
		this.f_name = f_name;
	}

	@SuppressWarnings("finally")
	private boolean copy(File src, File dst) {
		InputStream in = null;
		OutputStream out = null;
		boolean b = false;
		try {
			in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE);
			out = new BufferedOutputStream(new FileOutputStream(dst),
					BUFFER_SIZE);
			byte[] buffer = new byte[BUFFER_SIZE];
			int len = 0;
			while ((len = in.read(buffer)) > 0) {
				out.write(buffer, 0, len);
			}
			b = true;
		} catch (Exception e) {
			logger.error(e);
		} finally {
			if (null != in) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (null != out) {
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return b;
		}
	}

	public String destDir() {
		String path = ServletActionContext.getServletContext().getRealPath("/");
		String upload_dir = path + "upload"
				+ System.getProperty("file.separator");
		FileUtil.makedir(upload_dir);
		return upload_dir;
	}

	public String destFileName() {
		return this.getF_name() + "_" + System.currentTimeMillis() + ".zip";
	}

	public String getFilePath() {
		return destDir() + destFileName();
	}

	public String uploader() {
		String filepath = getFilePath();
		String destDir = destDir();

		logger.info("The dest file is " + filepath);

		File dstFile = new File(filepath);
		boolean b = false;

		b = copy(this.upload, dstFile);
		if (!b)
			return Action.INPUT;

		int ipid = Integer.parseInt(this.getPid().trim());
		int iuid = Integer.parseInt(this.getUid().trim());
		
		queue.putTask(new ZipTask(filepath, this.getC_dir().trim(), this
				.getC_version().trim(), ipid, destDir, iuid,this.getF_name().trim()));
		logger.info("Add new task");
		return Action.SUCCESS;

	}

	private final static Logger logger = Logger.getLogger(UploadAction.class);
	private final static ZipQueue queue = ZipQueue.getInstance();
}
